package com.doone.telmarket.domain.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.doone.telmarket.domain.Service;
import com.doone.telmarket.domain.ServicePage;
import com.doone.telmarket.domain.factory.ServiceFactory;
import com.doone.telmarket.domain.factory.ServicePageFactory;
import com.doone.telmarket.domain.ibator.ServiceClassify;
import com.doone.telmarket.domain.ibator.ServiceType;
import com.doone.telmarket.domain.service.ServiceOpration;
import com.doone.telmarket.infrastructure.dao.ServiceDAO;

/**
 * @package com.doone.telmarket.domain.ibator
 * @author 甘雨波
 * @description 增值业务操作服务层实现类
 * @date 2009-9-17
 */
@org.springframework.stereotype.Service("serviceOpration")
public class ServiceOprationImpl implements ServiceOpration {

	@Autowired
	private ServicePageFactory servicePageFactory;

	@Autowired
	private ServiceFactory serviceFactory;

	@Autowired
	private ServiceDAO serviceDao;

	/**
	 * @package com.doone.telmarket.domain.ibator
	 * @author 甘雨波
	 * @description 操作数据生成页面对象
	 * @date 2009-9-17
	 */
	public ServicePage oprationData(String serviceCode, String serviceCodeType, String serviceName,
			String serviceSummary, String serviceCost, String spName, String serviceState, String serviceTypeId,
			String serviceClassifyId, String recommandGrade, String serviceContent, String[] fileNames) {
		try {
			if (serviceCost != "") {
				serviceCost = Integer.parseInt(serviceCost) * 100 + "";//把前台传来的费用*100变成分
			}
			ServiceType serviceType = new ServiceType();
			serviceType.setServiceTypeId(Integer.parseInt(serviceTypeId));

			ServiceClassify serviceClassify = new ServiceClassify();
			serviceClassify.setServiceClassifyId(Integer.parseInt(serviceClassifyId));

			Service service = serviceFactory.createService(serviceCode, serviceCodeType, serviceName, serviceSummary,
					serviceCost, spName, serviceState, serviceType, serviceClassify);
			ServicePage servicePage = servicePageFactory.createServicePage(service, Integer.parseInt(recommandGrade),
					serviceContent, fileNames[0], fileNames[1], fileNames[2]);
			return servicePage;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	/**
	 * @package com.doone.telmarket.domain.ibator
	 * @author 甘雨波
	 * @description 根据类型获取增值业务
	 * @date 2009-9-23
	 */
	public List<Service> selectServiceByType(String serviceTypeId, String serviceClassifyId) {
		Service service = new Service();
		ServiceType serviceType = new ServiceType();
		ServiceClassify serviceClassify = new ServiceClassify();
		serviceType.setServiceTypeId(Integer.parseInt(serviceTypeId));
		serviceClassify.setServiceClassifyId(Integer.parseInt(serviceClassifyId));
		service.setServiceClassify(serviceClassify);
		service.setServiceType(serviceType);
		return serviceDao.getServiceList(service);
	}

	/**
	 * @package com.doone.telmarket.domain.ibator
	 * @author 甘雨波
	 * @description 保存增值业务
	 * @date 2009-9-17
	 */
	public String saveService(ServicePage servicePage) {
		serviceDao.insertService(servicePage.getService());
		serviceDao.insertServicePage(servicePage);
		return "S";
	}

	/**
	 * @package com.doone.telmarket.domain.ibator
	 * @author 甘雨波
	 * @description 根据ID查询增值业务
	 * @date 2009-9-17
	 */
	public Service selectServiceById(String serviceCode) {
		return serviceDao.selectServiceByPrimaryKey(serviceCode);
	}

	/**
	 * @package com.doone.telmarket.domain.ibator
	 * @author 甘雨波
	 * @description 删除增值业务
	 * @date 2009-9-17
	 */
	public int deleteServiceById(String id) {
		return serviceDao.deleteServiceById(id);
	}

	/**
	 * @package com.doone.telmarket.domain.ibator
	 * @author 甘雨波
	 * @description 删除增值业务PAGE对象
	 * @date 2009-9-17
	 */
	public ServicePage selectServicePageByServiceId(String serviceId) {
		return serviceDao.selectServicePageByPrimaryKey(serviceId);
	}

	/**
	 * @package com.doone.telmarket.domain.ibator
	 * @author 甘雨波
	 * @description 更新增值业务
	 * @date 2009-9-17
	 */
	public void updateService(ServicePage servicePage) {
		serviceDao.updateService(servicePage);
	}

}
